<!--
 *Author：jxx
 *Date：{Date}
 *Contact：283591387@qq.com
 *业务请在@/extension/co/co/DepartmentExpenseAssign.jsx或DepartmentExpenseAssign.vue文件编写
 *新版本支持vue或【表.jsx]文件编写业务,文档见:https://doc.volcore.xyz/docs/view-grid、https://doc.volcore.xyz/docs/web
 -->
<template>
    <div class="grid">
        <view-grid ref="grid" :columns="columns" :detail="detail" :details="details" :editFormFields="editFormFields"
            :editFormOptions="editFormOptions" :searchFormFields="searchFormFields" :searchFormOptions="searchFormOptions"
            :table="table" :extend="extend" :onInit="onInit" :onInited="onInited" :searchBefore="searchBefore"
            :searchAfter="searchAfter" :addBefore="addBefore" :updateBefore="updateBefore" :rowClick="rowClick"
            :modelOpenBefore="modelOpenBefore" :modelOpenAfter="modelOpenAfter">
            <!-- 自定义组件数据槽扩展，更多数据槽slot见文档 -->
            <template #gridHeader>
            </template>
        </view-grid>
        <vol-box :lazy="true" v-model="model1" title="成本费用分配" :width="700" :height="200" :padding="5">
            <div class="container">
                <div class="content">
                    <vol-form ref="form" :loadKey="true" :formFields="formFields" :formRules="formRules"
                        labelPosition='left' ,>
                        <div style="text-align: center; width: 100%; margin-top: 30px">
                            <el-steps style="max-width: 700px;" :active="isActive" process-status="wait"
                                finish-status="success" simple>
                                <el-step title="成本费用归集" />
                                <el-step title="部门费用分配" />
                                <el-step title="生产工单费用分配" />
                                <el-step title="完成" />
                            </el-steps>
                        </div>
                        <div style="text-align: right; width: 100%; margin-top: 40px">
                            <el-button type="primary" :disabled="firstDisabled" @click="beginExpenseAssgin">开始</el-button>
                            <!-- <el-button type="primary" :disabled="nextDisabled" @click="taskExpenseAssgin">下一步</el-button> -->
                            <el-button type="info" :disabled="completeDisabled" @click="closeExpenseAssgin">关闭</el-button>
                        </div>
                    </vol-form>
                </div>
            </div>
        </vol-box>
    </div>
</template>
<script setup lang="jsx">
import extend from "@/extension/co/co/DepartmentExpenseAssign.jsx";
import viewOptions from './DepartmentExpenseAssign/options.js'
import { ElLoading } from 'element-plus'
import { ref, reactive, getCurrentInstance, watch, onMounted } from "vue";
const grid = ref(null);
const { proxy } = getCurrentInstance()
//http请求，proxy.http.post/get
const { table, editFormFields, editFormOptions, searchFormFields, searchFormOptions, columns, detail, details } = reactive(viewOptions())

let gridRef;//对应[表.jsx]文件中this.使用方式一样
const model1 = ref(false)
const firstDisabled = ref(false)
const nextDisabled = ref(false)
const completeDisabled = ref(true)
const isActive = ref(1)
const nextBtnCount = ref(1)

//生成对象属性初始化
const onInit = async ($vm) => {
    gridRef = $vm;
    gridRef.buttons.splice(3, 0, {
        name: '成本费用分配', //按钮名称
        icon: 'el-icon-document',
        type: 'primary',
        plain: true,
        onClick: () => {
            model1.value = true
            //开始按钮默认激活
            firstDisabled.value = false
            //下一步默认不可用
            nextDisabled.value = true
            // 进度默认未激活
            isActive.value = 0

        }
    })
}
const beginExpenseAssgin = async () => {
    const loading = ElLoading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)',
    })

    isActive.value = 1

    let url = 'api/DepartmentExpenseAssign/DepartmentCost';
    proxy.http.post(url).then((result) => {
        gridRef.search();
        isActive.value = 2
        setTimeout(() => {
            loading.close()
            // 开始完成后禁用开始按钮
            firstDisabled.value = true
            // 开始完成后激活下一步按钮
            nextDisabled.value = false
            // 进度激活第一步
            proxy.$message.success("部门成本费用归集");
            taskExpenseAssgin()
        }, 2000)

    });

}

const taskExpenseAssgin = async () => {
    const loading = ElLoading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)',
    })
    let url = 'api/DepartmentExpenseAssign/TaskCost';
    proxy.http.post(url).then((result) => {
        gridRef.search();
        setTimeout(() => {
            loading.close()
            proxy.$message.success("部门费用分配");
            //部分费用分配完成激活第二步
            //下一步禁用
            nextDisabled.value = true
            // 激活关闭按钮
            completeDisabled.value = false
            isActive.value = 4

        }, 2000)

    })
}

const closeExpenseAssgin = async () => {
    model1.value = false
}
//生成对象属性初始化后,操作明细表配置用到
const onInited = async () => {
}
const searchBefore = async (param) => {
    //界面查询前,可以给param.wheres添加查询参数
    //返回false，则不会执行查询
    return true;
}
const searchAfter = async (rows, result) => {
    return true;
}
const addBefore = async (formData) => {
    //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
    return true;
}
const updateBefore = async (formData) => {
    //编辑保存前formData为对象，包括明细表、删除行的Id
    return true;
}
const rowClick = ({ row, column, event }) => {
    //查询界面点击行事件
    // grid.value.toggleRowSelection(row); //单击行时选中当前行;
}
const modelOpenBefore = async (row) => {//弹出框打开后方法
    return true;//返回false，不会打开弹出框
}
const modelOpenAfter = (row) => {
    //弹出框打开后方法,设置表单默认值,按钮操作等
}
//监听表单输入，做实时计算
//watch(() => editFormFields.字段,(newValue, oldValue) => {	})
//对外暴露数据
defineExpose({})
</script>
